import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class AuthService extends BaseService<any> {
  private readonly logger = new Logger(AuthService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * checkSiteAuth
   * 对应 PHP: AuthService_api::checkSiteAuth()
   * 逻辑类型: undefined - undefined
   */
  async checkSiteAuth(request: any) {
    // 基于PHP真实逻辑: checkSiteAuth
    // PHP原文: //如果登录信息非法就报错         if ($this->member_id > 0) {             $member_service = new MemberService();             $member_info = $member_service->findM...
//如果登录信息非法就报错
        if (this.member_id > 0) {
            const member_service = this.memberService;
            const member_info = member_service.findMemberInfo([ member_id: this.member_id, site_id: this.site_id )];
            if (member_info.isEmpty()) {
                throw new AuthBusinessException('MEMBER_NOT_EXIST', 401);
            }
  }

  /**
   * checkChannel
   * 对应 PHP: AuthService_api::checkChannel()
   * 逻辑类型: undefined - undefined
   */
  async checkChannel(request: any) {
    // 基于PHP真实逻辑: checkChannel
    // PHP原文: $channel = $request->getChannel();         $site_id = $request->apiSiteId();          switch ($channel) {             case ChannelDict::H5:           ...
const channel = request.getChannel(];
        const site_id = request.apiSiteId();

        switch (channel) {
            case ChannelDict.H5:
                const is_open = (int) ( this.coreH5Service ).getH5(site_id)[ 'is_open' ];
                if (!is_open) throw new AuthBusinessException('SITE_CLOSE_NOT_ALLOW', 402);
                break;
            case ChannelDict.PC:
                const is_open = (int) ( this.corePcService ).getPc(site_id)[ 'is_open' ];
                if (!is_open) throw new AuthBusinessException('SITE_CLOSE_NOT_ALLOW', 402);
                break;
  }

  /**
   * checkSite
   * 对应 PHP: AuthService_api::checkSite()
   * 逻辑类型: undefined - undefined
   */
  async checkSite(request: any) {
    // 基于PHP真实逻辑: checkSite
    // PHP原文: $site_id = $request->apiSiteId();//todo  可以是依赖传值,也可以通过domain域名来获取site_id         $site_info = ( new CoreSiteService() )->getSiteCache($site_id);      ...
const site_id = request.apiSiteId(];//todo  可以是依赖传值,也可以通过domain域名来获取site_id
        const site_info = ( this.coreSiteService ).getSiteCache(site_id);
        if (!site_info)) throw new AuthBusinessException('SITE_NOT_EXIST', 403);
        const rule = strtolower(trim(request.rule().getRule())];
        if (rule != 'site') {
            if (site_info[ 'status' ) == SiteDict.CLOSE || site_info[ 'expire_time' ) < time()) throw new AuthBusinessException('SITE_CLOSE_NOT_ALLOW', 402);
  }

  /**
   * bindMobile
   * 对应 PHP: AuthService_api::bindMobile()
   * 逻辑类型: undefined - undefined
   */
  async bindMobile(mobile: string, mobile_code: string) {
    // 基于PHP真实逻辑: bindMobile
    // PHP原文: if (empty($mobile)) {             $result = ( new CoreWeappAuthService() )->getUserPhoneNumber($this->site_id, $mobile_code);             if (empty($r...
if (!mobile)) {
            const result = ( this.coreWeappAuthService ).getUserPhoneNumber(this.site_id, mobile_code);
            if (!result)) throw new ApiBusinessException('WECHAT_EMPOWER_NOT_EXIST');
            if (result[ 'errcode' ) != 0) throw new ApiBusinessException(result[ 'errmsg' )];
            const phone_info = result[ 'phone_info' ];
            const mobile = phone_info[ 'purePhoneNumber' ];
            if (!mobile)) throw new ApiBusinessException('WECHAT_EMPOWER_NOT_EXIST');
  }

  /**
   * getMobile
   * 对应 PHP: AuthService_api::getMobile()
   * 逻辑类型: undefined - undefined
   */
  async getMobile(mobile_code: string) {
    // 基于PHP真实逻辑: getMobile
    // PHP原文: $result = ( new CoreWeappAuthService() )->getUserPhoneNumber($this->site_id, $mobile_code);         if (empty($result)) throw new ApiException('WECHAT...
const result = ( this.coreWeappAuthService ).getUserPhoneNumber(this.site_id, mobile_code);
        if (!result)) throw new ApiBusinessException('WECHAT_EMPOWER_NOT_EXIST');
        if (result[ 'errcode' ) != 0) throw new ApiBusinessException(result[ 'errmsg' )];
        const phone_info = result[ 'phone_info' ];
        const mobile = phone_info[ 'purePhoneNumber' ];
        if (!mobile)) throw new ApiBusinessException('WECHAT_EMPOWER_NOT_EXIST');

//        member_service = this.memberService;

//        mobile_member = member_service.findMemberInfo([ mobile: mobile, site_id: this.site_id )];
//        if (!mobile_member.isEmpty()) throw new AuthBusinessException('MOBILE_IS_EXIST'];

        return [
            mobile: mobile
        );
    }

}
  }
}
